<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header class="flex-spawen">
      <!-- logo -->
      <div class="logo flex-container">
        <img src="../../assets/upload/onePiece.png" alt="">
        <h1>OnePiece</h1>
      </div>
      <div class="btn">
        <el-button type="primary" @click.prevent="backLogin">退出登录</el-button>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? '60px' : '200px'">
        <div class="toggle-button" @click.stop="toggleCollapse">|||</div>
        <el-menu class="el-menu-vertical-demo" background-color="#222832" text-color="#fff" active-text-color="#409eff"
          :unique-opened="true" :collapse=isCollapse :collapse-transition="false" router :default-active="saveState">
          <!-- 第一个导航  一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单内容 -->
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span style="margin-left:15px">{{ item.authName }}</span>
            </template>
            <!-- 子菜单 -->
            <el-menu-item :index="'/' + child.path" v-for="child in item.children" :key="child.id"
              @click="saveNavState('/' + child.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ child.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
        <!-- 页底区域 -->
        <el-footer class="flex-spawen">
          <h1 style="color:#1eacac">©2023-2024 OnePiece. All rights reserved</h1>
        </el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'MyHome',
  data() {
    return {
      menuList: [],
      icons: {
        125: 'icon-user-tie',
        103: 'icon-key',
        101: 'icon-cart',
        102: 'icon-clipboard',
        145: 'icon-monitor'
      },
      isCollapse: false, // 是否折叠
      saveState: ''// 高亮导航
    }
  },
  created() {
    this.getMenuList()
    this.saveState = window.sessionStorage.getItem('activeSave')
  },
  methods: {
    backLogin() {
      // 退出登录
      this.$router.push('/login')
      window.sessionStorage.clear()
    },
    async getMenuList() {
      // 获取菜单数据
      const { data: res } = await this.$http.get('/menus')
      // 对获取到的数据进行判断
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.menuList = res.data
    },
    toggleCollapse() {
      // 点击按钮切换与展开
      this.isCollapse = !this.isCollapse
    },
    saveNavState(val) {
      // 高亮导航
      window.sessionStorage.setItem('activeSave', val)
      this.saveState = window.sessionStorage.getItem('activeSave')
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  width: 100%;
  height: 100vh;

  // 头部
  .el-header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background-color: #ffffff;
    padding: 0;
    z-index: 9;

    .logo {
      width: 200px;
      height: 100%;
      border-bottom: 1px solid rgba(250, 250, 250, .1);
      background-color: #222832;

      img {
        height: 60px;
        width: 60px;
        margin-right: 10px;
      }

      h1 {
        color: #1eacac;
        z-index: 444;
      }
    }

    .btn {
      margin-right: 30px;
    }
  }

  // 侧边栏
  .el-aside {
    position: fixed;
    top: 30px;
    left: 0;
    height: 100%;
    overflow: hidden;
    background-color: #222832;
    z-index: 8;

    .toggle-button {
      width: 100%;
      height: 30px;
      text-align: center;
      line-height: 30px;
      color: #ffff;
      font-size: 14px;
      letter-spacing: 2px;
      cursor: pointer;
      background-color: #475163;
    }

    .el-menu {
      border-right: 0;
    }
  }

  // 主体
  .el-main {
    background-color: #e9edf1;
    padding: 90px 50px 70px 250px;
  }

  // 底部
  .el-footer {
    position: fixed;
    bottom: 0;
    right: 0;
    width: 100%;
    background-color: #f5f2f2;
    z-index: 2;
    padding: 0px 230px;
  }
}
</style>
